from django.shortcuts import render,HttpResponse
from rest_framework.viewsets import GenericViewSet, ViewSetMixin
from rest_framework.mixins import ListModelMixin
from rest_framework.generics import ListAPIView
from django.core.cache import cache
from . import models
from . import serializer
from django.conf import settings   # 这个是django 的配置文件：优先用项目的配置信息，如果没有，使用内置的
from utils.response import APIResponse
from rest_framework.response import Response

# class BannerView(GenericViewSet,ListModelMixin):
class BannerView(ViewSetMixin, ListAPIView):
    queryset = models.Banner.objects.all().filter(is_delete=False, is_show=True).order_by('orders')[:settings.BANNER_COUNT]
    serializer_class = serializer.BannerSerializer


    def list(self, request, *args, **kwargs):
        banner_list = cache.get('banner_list_cache')
        if not banner_list:
            print('查询数据库')
            res = super().list(request, *args, **kwargs)
            banner_list = res.data
            cache.set('banner_list_cache',banner_list)

        return Response(data=banner_list)




def index(request):
    print(request.method)
    print('put执行了')
    return HttpResponse('ssss')

